# -*- coding: utf-8 -*-
"""
@Time ： 2022-07-21 9:52
@Auth ： Mr.Z
@Company ：test
@Function ：jsonpath提取信息
"""

# https://blog.csdn.net/qq_44614026/article/details/124429067

from jsonpath import  jsonpath

a = {'status': 200, 'msg': 'success', 'token': '7f5f35ee15f44820b54a163272da212e'}
res =jsonpath(a,"status")[0]
print(res)

res = {
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 9
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    }
}

# 过滤出所有的包含isbn的书信息，?(): 条件过滤  @ 代表当前元素
book = jsonpath(res,r'$.store.book[?(@.isbn)]')
print(book)

# 获取店内所有书籍的作者, * 匹配所有元素节点
auth = jsonpath(res,"$.store.book[*].author")
print(auth)

print('获取所有作者')
print(jsonpath(res,"$...author"))

# 过滤出价格低于9的书
print(jsonpath(res,"$.store.book[?(@.price<9)]"))

# 过滤出价格等于9的书
print(jsonpath(res,"$.store.book[?(@.price==9)]"))

# 过滤title=='Moby Dick'的书
print(jsonpath(res,"$.store.book[?(@.title=='Moby Dick')]"))
print(type(auth))
print(type(res))

